﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Top.Api;
using Top.Api.Domain;
using Top.Api.Parser;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Util;
using DBControl;

namespace Newsell
{
    public class ITopControl
    {
        private static string appkey = ConfigurationManager.AppSettings["AppKey_EvoVIP"].ToString();
        private static string appsecret = ConfigurationManager.AppSettings["AppSecret_EvoVIP"].ToString();
        private static string appurl = ConfigurationManager.AppSettings["AppUrl_EvoVIP"].ToString();

        private static ITopClient client = new DefaultTopClient(appurl, appkey, appsecret);
        
        /// <summary>
        /// SellercatsListGetResponse
        /// </summary>
        private SellercatsListGetResponse scListgetRes;
        private ItemsOnsaleGetResponse onSellRes;
        private ItemGetResponse itemGetRes;
        private UserCredit uSellerCredit;

        public SellercatsListGetResponse sclistres{ get { return scListgetRes; }}
        public ItemsOnsaleGetResponse onsellres { get { return onSellRes; } }
        public ItemGetResponse itemgetres { get { return itemGetRes; } }
        public UserCredit usellercredit { get { return uSellerCredit; } }

        /// <summary>
        /// 取卖家自定义类目
        /// </summary>
        /// <param name="usNick">用户昵称</param>
        public void sellcatGet(string usNick)
        {
            //ITopClient clientjson = new DefaultTopClient(SystemLib.appurl, "12224018", "sandbox2629b8ce8199f92ab8300488f", "json");
            SellercatsListGetRequest scListreq = new SellercatsListGetRequest();
            scListreq.Nick = usNick;
            scListreq.Fields = "cid,name,parent_cid,sort_order";
            scListgetRes = client.Execute(scListreq);
        }

        /// <summary>
        /// 取出售中商品
        /// </summary>
        /// <param name="sessionKey">sessionkey</param>
        /// <param name="pagesize">每页条数,0为默认值</param>
        /// <param name="pageno">页码，0为默认值</param>
        public void onsellsGet(string sessionKey, int pagesize, int pageno)
        {
            ItemsOnsaleGetRequest onsellreq = new ItemsOnsaleGetRequest();
            if (pagesize == 0) { pagesize = 5; }
            if (pageno == 0) { pageno = 1; }
            #region 查询字段: 数字id,标题,价格,自定义类目,图片链接,剩余数量,商品链接,有效期,
            onsellreq.Fields = "num_iid,title,price,pic_url,seller_cids,num,valid_thru";
            //req.Q = "N97";
            //req.Cid = 1512L;
            //req.SellerCids = "11";
            //req.PageNo = 10L;
            //req.HasDiscount = true;
            //req.HasShowcase = true;
            //req.OrderBy = "list_time:desc";
            //req.IsTaobao = true;
            //req.IsEx = true;
            onsellreq.PageSize = pagesize;
            onsellreq.PageNo = pageno;
            //DateTime dateTime = DateTime.Parse("2000-01-01 00:00:00");
            //req.StartModified = dateTime;
            //DateTime dateTime = DateTime.Parse("2000-01-01 00:00:00");
            //req.EndModified = dateTime;
            #endregion 查询字段
            onSellRes = client.Execute(onsellreq, sessionKey);
        }
        /// <summary>
        /// 取出售中商品
        /// </summary>
        /// <param name="sessionKey">sessionkey</param>
        /// <param name="cid">类目ID</param>
        /// <param name="pagesize">每页条数，0为默认值</param>
        /// <param name="pageno">页码，0为默认值</param>
        public void onsellsGet(string sessionKey, int pagesize, int pageno, string cid )
        {
            ItemsOnsaleGetRequest onsellreq = new ItemsOnsaleGetRequest();
            onsellreq.Fields = "num_iid,title,price,pic_url,seller_cids,num,valid_thru";
            onsellreq.SellerCids = cid;
            if (pagesize == 0){pagesize = 5;}
            if (pageno == 0) { pageno = 1; }
            onsellreq.PageSize = pagesize;
            onsellreq.PageNo = pageno;
            onSellRes = client.Execute(onsellreq, sessionKey);
        }

        /// <summary>
        /// 取单个商品信息
        /// </summary>
        /// <param name="sessionkey">sessionkey</param>
        /// <param name="numid">数字id</param>
        public void itemGet(string sessionkey, string numid)
        {
            ItemGetRequest req = new ItemGetRequest();
            req.NumIid = Convert.ToInt64(numid);
            req.Fields = "num_iid,title,price,detail_url,type,pic_url,num,stuff_status,post_fee,express_fee,ems_fee,item_imgs.url,location";
            itemGetRes = client.Execute(req, sessionkey);
        }
        /// <summary>
        /// 取单个商品信息
        /// </summary>
        /// <param name="sessionkey">sessionkey</param>
        /// <param name="numid">数字id</param>
        /// <param name="unick">用户昵称</param>
        public void itemGet(string sessionkey, string numid, string unick)
        {
            ItemGetRequest req = new ItemGetRequest();
            req.Nick = unick;
            req.NumIid = Convert.ToInt64(numid);
            req.Fields = "num_iid,title,price,detail_url,type,pic_url,num,stuff_status,post_fee,express_fee,ems_fee,item_imgs.url,location";
            itemGetRes = client.Execute(req, sessionkey);
        }

        /// <summary>
        /// 获取用户信誉等级
        /// </summary>
        /// <param name="unick">用户昵称</param>
        /// <param name="sessionkey">sessionKey</param>
        /// <returns></returns>
        public void usCreditGet(string unick, string sessionkey)
        {
            UserGetRequest req = new UserGetRequest();
            req.Fields = "seller_credit";
            req.Nick = unick;
            UserGetResponse uGetres = client.Execute(req, sessionkey);
            uSellerCredit = uGetres.User.SellerCredit;
        }
        /// <summary>
        /// 获取卖家店铺类目
        /// </summary>
        public long shopCatGet(string usnick)
        {
            ShopGetRequest req = new ShopGetRequest();
            req.Fields = "cid";
            req.Nick = usnick;
            ShopGetResponse response = client.Execute(req);
            if (response.Shop != null)
            {
                return response.Shop.Cid;
            }
            else{return 0;}

        }
    }

}